package com.example.blog.controller;

import com.example.blog.dto.ContentAuditDTO;
import com.example.blog.service.ContentAuditService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/audits")
@CrossOrigin(origins = "*", maxAge = 3600)
public class ContentAuditController {
    @Autowired
    private ContentAuditService contentAuditService;

    @PostMapping
    @PreAuthorize("hasRole('USER')")
    public ResponseEntity<ContentAuditDTO> submitForAudit(@RequestBody ContentAuditDTO auditDTO) {
        return ResponseEntity.ok(contentAuditService.submitForAudit(auditDTO));
    }

    @PostMapping("/{id}/approve")
    @PreAuthorize("hasRole('ADMIN')")
    public ResponseEntity<ContentAuditDTO> approve(
            @PathVariable Long id,
            @RequestParam(required = false) String reason) {
        return ResponseEntity.ok(contentAuditService.approve(id, reason));
    }

    @PostMapping("/{id}/reject")
    @PreAuthorize("hasRole('ADMIN')")
    public ResponseEntity<ContentAuditDTO> reject(
            @PathVariable Long id,
            @RequestParam String reason) {
        return ResponseEntity.ok(contentAuditService.reject(id, reason));
    }

    @GetMapping("/{id}")
    @PreAuthorize("hasRole('USER')")
    public ResponseEntity<ContentAuditDTO> getAuditById(@PathVariable Long id) {
        return ResponseEntity.ok(contentAuditService.getAuditById(id));
    }

    @GetMapping("/pending")
    @PreAuthorize("hasRole('ADMIN')")
    public ResponseEntity<Page<ContentAuditDTO>> getPendingAudits(Pageable pageable) {
        return ResponseEntity.ok(contentAuditService.getPendingAudits(pageable));
    }

    @GetMapping("/status/{status}")
    @PreAuthorize("hasRole('ADMIN')")
    public ResponseEntity<Page<ContentAuditDTO>> getAuditsByStatus(
            @PathVariable String status,
            Pageable pageable) {
        return ResponseEntity.ok(contentAuditService.getAuditsByStatus(status, pageable));
    }

    @GetMapping("/submitter/{submitterId}")
    @PreAuthorize("hasRole('USER')")
    public ResponseEntity<Page<ContentAuditDTO>> getAuditsBySubmitter(
            @PathVariable Long submitterId,
            Pageable pageable) {
        return ResponseEntity.ok(contentAuditService.getAuditsBySubmitter(submitterId, pageable));
    }

    @GetMapping("/auditor/{auditorId}")
    @PreAuthorize("hasRole('ADMIN')")
    public ResponseEntity<Page<ContentAuditDTO>> getAuditsByAuditor(
            @PathVariable Long auditorId,
            Pageable pageable) {
        return ResponseEntity.ok(contentAuditService.getAuditsByAuditor(auditorId, pageable));
    }

    @GetMapping("/content")
    @PreAuthorize("hasRole('USER')")
    public ResponseEntity<Page<ContentAuditDTO>> getAuditsByContent(
            @RequestParam String type,
            @RequestParam Long contentId,
            Pageable pageable) {
        return ResponseEntity.ok(contentAuditService.getAuditsByContent(type, contentId, pageable));
    }

    @GetMapping("/content/status")
    @PreAuthorize("hasRole('USER')")
    public ResponseEntity<Boolean> isContentUnderAudit(
            @RequestParam String type,
            @RequestParam Long contentId) {
        return ResponseEntity.ok(contentAuditService.isContentUnderAudit(type, contentId));
    }
} 